package com.yang.leather.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yang.leather.admin.dao.IAdminUserDao;
import com.yang.leather.admin.pojo.AdminUser;
import com.yang.leather.admin.service.IAdminUserService;
import com.yang.leather.common.util.PwdEncoder;
import com.yang.leather.core.LeatherCoreServiceImpl;

@Service("adminUserService")
@Transactional
public class AdminUserServiceImpl extends LeatherCoreServiceImpl<AdminUser> implements IAdminUserService{
	
	private PwdEncoder pwdEncoder;
	
	public AdminUser authenticate(String account, String password) {
		AdminUser user = getByAccount(account);
		if (user != null) {
			String md5Pwd = pwdEncoder.encodePassword(password);
			if (md5Pwd.equals(user.getPassword())) {
				return user;
			}
		}
		return null;
	}
	
	public void updatePassword(Long id, String newPwd) {
		AdminUser user = findById(id);
		user.setPassword(pwdEncoder.encodePassword(newPwd));
		update(user);
	}
	
	public AdminUser getByAccount(String account) {
		return getDao().getByAccount(account);
	}
	
	public PwdEncoder getPwdEncoder() {
		return pwdEncoder;
	}

	@Autowired
	public void setPwdEncoder(@Qualifier("pwdEncoder")
			PwdEncoder pwdEncoder) {
		this.pwdEncoder = pwdEncoder;
	}

	@Autowired
	public void setDao(@Qualifier("adminUserDao")
			IAdminUserDao dao){
		super.setDao(dao);
	}
	
	public IAdminUserDao getDao(){
		return (IAdminUserDao)super.getDao();
	}
}
